const __extends = (this && this.__extends) || (function() {
  var extendStatics = function(d, b) {
    extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function(d, b) { d.__proto__ = b }) ||
            function(d, b) { for (const p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p] }
    return extendStatics(d, b)
  }
  return function(d, b) {
    if (typeof b !== 'function' && b !== null) { throw new TypeError('Class extends value ' + String(b) + ' is not a constructor or null') }
    extendStatics(d, b)
    function __() { this.constructor = d }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __())
  }
})()
let MyStorage;
(function(MyStorage) {
  const AbsStorage = (function() {
    function AbsStorage() {
    }
    AbsStorage.prototype.formatSize = function(byte) {
      if (byte < 1024 * 1024) {
        return (byte / 1024).toFixed(2) + 'KB'
      } else {
        return (byte / (1024 * 1024)).toFixed(2) + 'MB'
      }
    }
    return AbsStorage
  }())
  const ls = (function(_super) {
    __extends(ls, _super)
    function ls() {
      return _super !== null && _super.apply(this, arguments) || this
    }
    ls.prototype.get = function(name) {
      return localStorage.getItem(name)
    }
    ls.prototype.set = function(name, value) {
      localStorage.setItem(name, value)
    }
    ls.prototype.remove = function(name) {
      localStorage.removeItem(name)
    }
    ls.prototype.clear = function() {
      for (let i = 0; i < localStorage.length; i++) {
        const key = localStorage.key(i)
        if (key !== null) {
          if (key.substring(0, 5) === 'erp3_') {
            this.remove(key)
            i--
          }
        } else {
        }
      }
    }
    ls.prototype.forceClear = function() {
      localStorage.clear()
    }
    ls.prototype.usageSize = function() {
      let size = 0
      for (let i = 0; i < localStorage.length; i++) {
        const key = localStorage.key(i)
        if (key !== null && key.substring(0, 5) === 'erp3_') {
          const val = localStorage.getItem(key)
          size += key.length + (val ? val.length : 0)
        }
      }
      return this.formatSize(size)
    }
    return ls
  }(AbsStorage))
  MyStorage.ls = ls
})(MyStorage || (MyStorage = {}))
const Adapter = (function() {
  function Adapter() {
  }
  Adapter.getKey = function(name) {
    return this.prefix + name
  }
  Adapter.get = function(name) {
    const preName = this.getKey(name)
    let data
    const storageFactory = new MyStorage[this.engine]()
    data = Reflect.apply(storageFactory.get, storageFactory, [preName])
    if (!data) {
      return null
    }
    data = JSON.parse(data)
    if ((data.expireAt !== 0 && new Date() > new Date(data.expireAt)) || (this.version !== '' && data.version !== this.version)) {
      this.remove(name)
      return null
    }
    return data.data
  }
  Adapter.set = function(name, value, expireTime, version) {
    if (expireTime === void 0) { expireTime = this.expireTime }
    if (version === void 0) { version = this.version }
    const save = {
      expireAt: expireTime === 0 ? 0 : (new Date()).getTime() + expireTime * 1000,
      version: version,
      data: value
    }
    const _save = JSON.stringify(save)
    const storageFactory = new MyStorage[this.engine]()
    return Reflect.apply(storageFactory.set, storageFactory, [this.getKey(name), _save])
  }
  Adapter.remove = function(name) {
    const storageFactory = new MyStorage[this.engine]()
    return Reflect.apply(storageFactory.remove, storageFactory, [this.getKey(name)])
  }
  Adapter.clear = function() {
    const storageFactory = new MyStorage[this.engine]()
    return Reflect.apply(storageFactory.clear, storageFactory, [])
  }
  Adapter.expire = function(time) {
    this.expireTime = time
    return this
  }
  Adapter.v = function(version) {
    if (version) {
      this.version = version
      this.set('v', version)
    } else {
      return this.get('v')
    }
  }
  Adapter.usageSize = function() {
    const storageFactory = new MyStorage[this.engine]()
    return Reflect.apply(storageFactory.usageSize, storageFactory, [])
  }
  Adapter.engine = 'ls'
  Adapter.expireTime = 0
  Adapter.prefix = 'erp3_'
  Adapter.version = ''
  return Adapter
}())
export default Adapter
